package com.asktgapp.user.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.asktgapp.JGTool.EmojiFilter;
import com.asktgapp.JGTool.JGColor;
import com.asktgapp.JGTool.Utils;
import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.application.MyApplication;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.model.CollectionVO;
import com.asktgapp.model.FreeAskDetailVO;
import com.asktgapp.model.FreeAskSearchVO;
import com.asktgapp.modulebase.Constant;
import com.asktgapp.modulebase.ServiceUtil;
import com.asktgapp.modulebase.common.adapter.BaseAdapter;
import com.asktgapp.modulebase.common.adapter.BaseViewHolder;
import com.asktgapp.modulebase.common.util.PreferencesUtil;
import com.asktgapp.modulebase.common.util.Util;
import com.asktgapp.user.activity.EngineerDetailActivity;
import com.asktgapp.user.activity.FastPaymentActivity;
import com.asktgapp.user.activity.FreeAskCommentActivity;
import com.asktgapp.user.activity.PhotoViewActivity;
import com.asktgapp.user.activity.ShareActivity;
import com.asktgapp.utils.ImageDisplayUtil;
import com.sobot.chat.utils.ToastUtil;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * time:2018-7-21 21:17:13
 * Description:快速提问问答详情页面
 */

public class FreeAskAnswerDetailFragment extends BaseFragment {
    private TextView myBack, myToolbarTitle, commit, tvNoAnswer, tvAskName,
            tvTotalZan, addTime, tv_answer, toUserName,editLine;
    private RecyclerView mRecyclerView;
    private BaseAdapter mBaseAdapter;
    private ImageView tvShare, tvZan, ivHead, ivZan, ivQuestionImg1, ivQuestionImg2, ivQuestionImg3;
    private EditText etComment;
    private String mQuestionId, questionTemp, toUserId, answerId,deviceId;
    private LinearLayout llQuestionImg,llEdit;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_free_ask_answer_detail, container, false);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().getToolbar().setVisibility(View.GONE);
        mQuestionId = getActivity().getIntent().getStringExtra("id");
        deviceId = getActivity().getIntent().getStringExtra("deviceId");
        myBack = rootView.findViewById(R.id.myBack);
        myToolbarTitle = rootView.findViewById(R.id.myToolbarTitle);
        mRecyclerView = rootView.findViewById(R.id.recycleHot);
        commit = rootView.findViewById(R.id.commit);
        tvShare = rootView.findViewById(R.id.tvShare);
        tvZan = rootView.findViewById(R.id.tvZan);
        etComment = rootView.findViewById(R.id.etComment);
        tvNoAnswer = rootView.findViewById(R.id.tvNoAnswer);
        tvAskName = rootView.findViewById(R.id.tvAskName);
        tvTotalZan = rootView.findViewById(R.id.tvTotalZan);
        addTime = rootView.findViewById(R.id.addTime);
        ivHead = rootView.findViewById(R.id.iv_head_ask);
        ivZan = rootView.findViewById(R.id.ivZan);
        toUserName = rootView.findViewById(R.id.toUserName);
        editLine = rootView.findViewById(R.id.editLine);
        tv_answer = rootView.findViewById(R.id.tv_answer);
        llQuestionImg = rootView.findViewById(R.id.llQuestionImg);
        ivQuestionImg1 = rootView.findViewById(R.id.ivQuestionImg1);
        ivQuestionImg2 = rootView.findViewById(R.id.ivQuestionImg2);
        ivQuestionImg3 = rootView.findViewById(R.id.ivQuestionImg3);
        llEdit = rootView.findViewById(R.id.llEdit);


        etComment.setFilters(new InputFilter[]{new EmojiFilter()});
        myBack.setOnClickListener(this);
        commit.setOnClickListener(this);
        tvShare.setOnClickListener(this);
        tvZan.setOnClickListener(this);
        ivZan.setOnClickListener(this);

        ivQuestionImg1.setOnClickListener(this);
        ivQuestionImg2.setOnClickListener(this);
        ivQuestionImg3.setOnClickListener(this);
        tv_answer.setOnClickListener(this);
        etComment.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    commit.setVisibility(View.VISIBLE);
                } else {
                    commit.setVisibility(View.GONE);
                }
            }
        });

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.addItemDecoration(new DefaultItemDecoration(JGColor.APP_BACKGROUND));

        mBaseAdapter = new BaseAdapter(getActivity()) {
            @Override
            public BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
                BaseViewHolder holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_free_ask_img0, parent, false),
                        new BaseViewHolder.Callbacks() {
                            @Override
                            public void bind(BaseViewHolder holder, final Object data) {

                                LinearLayout allAnswer = holder.getView(R.id.llAnswerComment);
                                ImageView ivZan = holder.getView(R.id.ivZan);
                                ImageView ivAskHead = holder.getView(R.id.iv_head_ask);

                                if (data instanceof FreeAskSearchVO.AnswerListBean) {
                                    final FreeAskSearchVO.AnswerListBean bean = (FreeAskSearchVO.AnswerListBean) data;
                                    allAnswer.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            if (checkIsLogin("登录后可查看更多内容")) {
                                                Intent intent = new Intent(getActivity(), FreeAskCommentActivity.class);
                                                intent.putExtra("id", bean.getId());
                                                intent.putExtra("answerBean", bean);
                                                getActivity().startActivity(intent);
                                            }
                                        }
                                    });
                                    ImageDisplayUtil.loadCircleHeadImage(getActivity(), ivAskHead, bean.getUserPic1());
                                    Date date = Util.StrToDate(bean.getAddTime(), "yyyy.MM.dd HH:mm:ss");
                                    String timeStr = Utils.getAskTime(date);
                                    holder.setText(R.id.addTime, timeStr);

                                    holder.setText(R.id.tv_answer, bean.getContent());
                                    holder.setText(R.id.tvAskName, bean.getUsername1());
                                    holder.setText(R.id.tvZan, bean.getLikeNumber());
                                    if (bean.getIsProjector().equals("1")) {
                                        holder.getView(R.id.zan).setBackgroundResource(R.drawable.bg_circle_gray_item);
                                        holder.getView(R.id.tvReward).setVisibility(View.VISIBLE);
                                        holder.setText(R.id.tvReward,"赏 "+bean.getRewardTimes());
                                        holder.getView(R.id.tvAnswerType).setVisibility(View.VISIBLE);
                                        holder.getView(R.id.tvReward).setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                if (checkIsLogin("登录后可查看更多内容")) {
                                                    if (bean.getAuserId().equals(PreferencesUtil.getString(getContext(), PreferencesUtil.USER_ID))) {
                                                        showTost("不可以给自己打赏", 1);
                                                        return;
                                                    }
                                                    Intent intent = new Intent(getActivity(), FastPaymentActivity.class);
                                                    intent.putExtra("isReward", true);
                                                    intent.putExtra("receivables", bean);
                                                    intent.putExtra("label", "");
                                                    startActivity(intent);
                                                }
                                            }
                                        });
                                        String userId = PreferencesUtil.getString(getContext(), PreferencesUtil.USER_ID);
                                        if (bean.getAuserId().equals(userId)) {
                                            holder.getView(R.id.delete).setVisibility(View.VISIBLE);
                                            holder.getView(R.id.delete).setOnClickListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View v) {
                                                    delAnswerOrDiscusss(bean.getId());
                                                }
                                            });
                                        } else {
                                            holder.getView(R.id.delete).setVisibility(View.GONE);
                                        }
                                        ivAskHead.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                if (checkIsLogin("登录后可查看更多内容")) {
                                                    Intent intent = new Intent(getActivity(), EngineerDetailActivity.class);
                                                    intent.putExtra("id", bean.getAuserId());
                                                    startActivity(intent);
                                                }

                                            }
                                        });

                                        holder.getView(R.id.ask).setVisibility(View.VISIBLE);
                                        holder.getView(R.id.ask).setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                if (checkIsLogin("登录后可查看更多内容")) {
                                                    Intent intent = new Intent(getActivity(), EngineerDetailActivity.class);
                                                    intent.putExtra("id", bean.getAuserId());
                                                    startActivity(intent);
                                                }
                                            }
                                        });
                                    } else {
                                        holder.getView(R.id.zan).setBackgroundResource(R.color.white);
                                        holder.getView(R.id.tvReward).setVisibility(View.GONE);
                                        holder.getView(R.id.tvAnswerType).setVisibility(View.GONE);
                                        holder.getView(R.id.delete).setVisibility(View.GONE);
                                    }
                                    if (TextUtils.isEmpty(bean.getLikeId()))
                                        ivZan.setImageResource(R.mipmap.zan_2);
                                    else {
                                        ivZan.setImageResource(R.mipmap.zan_1);
                                    }

                                    if (bean.getDiscussList() != null && bean.getDiscussList().size() > 0) {
                                        holder.getView(R.id.llAnswerComment).setVisibility(View.VISIBLE);
                                        holder.getView(R.id.answerNumber).setVisibility(View.VISIBLE);
                                        holder.setText(R.id.answerNumber, bean.getDiscussList().size() + "回复");
                                        holder.setText(R.id.tvAnswerName, bean.getDiscussList().get(0).getUsername1());
                                        holder.setText(R.id.tvAnswerContent, bean.getDiscussList().get(0).getDiscussContent());
                                        holder.getView(R.id.tvCommentType).setVisibility(View.GONE);

                                        if (bean.getDiscussList().size() > 1) {
                                            holder.getView(R.id.llAnswer2).setVisibility(View.VISIBLE);
                                            holder.setText(R.id.tvAnswerName2, bean.getDiscussList().get(1).getUsername1());
                                            holder.setText(R.id.tvAnswerContent2, bean.getDiscussList().get(1).getDiscussContent());
                                            //判断是否工程师
//                                            if (bean.getDiscussList().get(1).get().equals("1")) {
//                                                holder.getView(R.id.tvCommentType2).setVisibility(View.VISIBLE);
//                                            } else {
//                                                holder.getView(R.id.tvCommentType2).setVisibility(View.INVISIBLE);
//                                            }
                                        } else {
                                            holder.getView(R.id.llAnswer2).setVisibility(View.GONE);
                                        }
                                    } else {
                                        holder.getView(R.id.llAnswerComment).setVisibility(View.GONE);
                                        holder.getView(R.id.answerNumber).setVisibility(View.GONE);
                                    }


                                    holder.getView(R.id.zan).setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            //点赞接口
                                            if (TextUtils.isEmpty(bean.getLikeId())) {
                                                dealZan(bean.getId(), "1", 1);
                                            } else {
                                                dealZan(bean.getId(), "0", 1);
                                            }
                                        }
                                    });
                                }
                            }

                            @Override
                            public void onItemClick(View view, int position) {
                                FreeAskSearchVO.AnswerListBean answerListBean = (FreeAskSearchVO.AnswerListBean) mBaseAdapter.getData(position);
//                                toUserId = answerListBean.getAuserId();
//                                answerId = answerListBean.getId();
//                                toUserName.setText("回复" + answerListBean.getUsername1() + "：");
                                if (toUserId != null && toUserId.equals(String.valueOf(answerListBean.getAuserId()))) {
                                    toUserId = null;
                                    answerId = null;
                                    etComment.setHint("写评论");
                                } else {
                                    toUserId = String.valueOf(answerListBean.getAuserId());
                                    answerId = answerListBean.getId();
                                    etComment.setHint("回复" + answerListBean.getUsername1());
                                }
//                                CollectionVO bean = (CollectionVO) mBaseAdapter.getData().get(position);
//                                Intent intent = new Intent(getActivity(), IndustryInfoDetailActivity.class);
//                                intent.putExtra("id", bean.getId());
//                                startActivity(intent);
                            }
                        });


                return holder;
            }
        };
        mRecyclerView.setAdapter(mBaseAdapter);

//        List<CollectionVO> list = new ArrayList<>();
//        for (int i = 0; i < 10; i++) {
//            list.add(new CollectionVO());
//        }
//
//        mBaseAdapter.setData(list);

    }

    private boolean commiting=false;
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tvShare:
                Intent intent = new Intent(getActivity(), ShareActivity.class);
                intent.putExtra("type", 3);
                intent.putExtra("id", mQuestionId);
                startActivity(intent);
                break;
            case R.id.myBack:
                getActivity().onBackPressed();
                break;
            case R.id.tvZan:
                dealZan(mQuestionId, questionTemp, 2);
                break;
            case R.id.commit:
                if(commiting){
                    return;
                }
                commiting = true;
                if (TextUtils.isEmpty(toUserId))
                    upAnswer();
                else
                    addDiscuss();
                break;
            case R.id.ivQuestionImg1:
                lookImage =true;
                PhotoViewActivity.statr(getActivity(), 0, images);
                break;
            case R.id.ivQuestionImg2:
                lookImage =true;
                PhotoViewActivity.statr(getActivity(), 1, images);
                break;
            case R.id.ivQuestionImg3:
                lookImage =true;
                PhotoViewActivity.statr(getActivity(), 2, images);
                break;
            case R.id.tv_answer:
                toUserId = null;
                answerId = null;
                toUserName.setText("");
                break;
            case R.id.ivZan:
                dealZan(mQuestionId, questionTemp, 2);
                break;
        }
    }

    @Override
    public void loadData() {
        showProgress(getClass().getSimpleName());

    }

    @Override
    public void onResume() {
        super.onResume();
        if (!lookImage)
            selProbleList();
        else
            lookImage =false;
    }

    ArrayList<String> images;
    private boolean lookImage = false;

    private void selProbleList() {

        Apiservice apiservice = ApiUtil.getInstance().create();
        HashMap params = new HashMap();
//        ApiUtil.putDefultData(getActivity(), params);
        params.put("userId", PreferencesUtil.getString(getContext(), PreferencesUtil.USER_ID));
        params.put("platform", "0");
        params.put("deviceId", deviceId);
        params.put("page", 1);
        params.put("id", mQuestionId);
        params.put("isHot", "0");
        if(TextUtils.isEmpty(deviceId)){
            ToastUtil.showToast(getActivity(),"设备唯一标识符异常");
            return;
        }
        Call<ApiResponseBody<FreeAskDetailVO>> call = apiservice.selProbleDetail(params);
        call.enqueue(new Callback<ApiResponseBody<FreeAskDetailVO>>() {
            @Override
            public void onResponse(Call<ApiResponseBody<FreeAskDetailVO>> call, Response<ApiResponseBody<FreeAskDetailVO>> response) {
                dismissProgress();
                if (response.isSuccessful()) {
                    FreeAskDetailVO bean = response.body().getResult();
                    if (bean != null) {
                        tvNoAnswer.setVisibility(View.GONE);
                        tvAskName.setText(bean.getProbleDetail().getUsername());
                        tvTotalZan.setText(bean.getProbleDetail().getProblemLikeNum());
                        ImageDisplayUtil.loadCircleHeadImage(getActivity(), ivHead, bean.getProbleDetail().getUserPic());

                        if (TextUtils.isEmpty(bean.getProbleDetail().getLikeId())) {
                            tvZan.setImageResource(R.mipmap.zan_2);
                            ivZan.setImageResource(R.mipmap.zan_2);

                            questionTemp = "1";
                        } else {
                            questionTemp = "0";
                            tvZan.setImageResource(R.mipmap.zan_1);
                            ivZan.setImageResource(R.mipmap.zan_1);
                        }
                        Date date = Util.StrToDate(bean.getProbleDetail().getAddTime(), "yyyy.MM.dd HH:mm:ss");
                        String timeStr = Utils.getAskTime(date);
                        addTime.setText(timeStr);
                        tv_answer.setText(bean.getProbleDetail().getContent());
                        images = (ArrayList<String>) bean.getProbleDetail().getImgList();

                        if (images != null && images.size() > 0) {
                            llQuestionImg.setVisibility(View.VISIBLE);
                            if (images.size() == 1) {
                                ImageDisplayUtil.LoadNetImage(ivQuestionImg1, getActivity(), images.get(0));
                                ivQuestionImg2.setVisibility(View.GONE);
                                ivQuestionImg3.setVisibility(View.GONE);
                            } else if (images.size() == 2) {
                                ImageDisplayUtil.LoadNetImage(ivQuestionImg1, getActivity(), images.get(0));
                                ImageDisplayUtil.LoadNetImage(ivQuestionImg2, getActivity(), images.get(1));
                                ivQuestionImg3.setVisibility(View.GONE);
                            } else {
                                ImageDisplayUtil.LoadNetImage(ivQuestionImg1, getActivity(), images.get(0));
                                ImageDisplayUtil.LoadNetImage(ivQuestionImg2, getActivity(), images.get(1));
                                ImageDisplayUtil.LoadNetImage(ivQuestionImg3, getActivity(), images.get(2));
                            }
                        } else {
                            llQuestionImg.setVisibility(View.GONE);
                        }
                        if(bean.getAnswerList()!=null&&bean.getAnswerList().size()>0){
                            llEdit.setVisibility(View.GONE);
                            editLine.setVisibility(View.GONE);
                        }else {
                            if(!PreferencesUtil.getBoolean(getContext(), PreferencesUtil.KEY_IS_ENGINEER)){
                                llEdit.setVisibility(View.GONE);
                                editLine.setVisibility(View.GONE);
                            }
                        }
                        mBaseAdapter.setData(bean.getAnswerList());
                    } else {
                        tvNoAnswer.setVisibility(View.VISIBLE);
                    }
                } else {
                    showTost(response.raw().message(), 1);
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody<FreeAskDetailVO>> call, Throwable t) {
                dismissProgress();
                ApiException apiException = ExceptionHandle.handleException(t);
                if (apiException.isTokenInvalid()) {
                    showUnLoginSnackbar();
                } else if (apiException.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else {
                    showTost(apiException.getMessage(), 0);
                }
            }
        });
    }

    //点赞接口
    private void dealZan(String id, String temp, int type) {
        if (Util.isEmpty(PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID))) {
            showTost("未登录，不能点赞！请先登录", 1);
            return;
        }
        if (Util.isEmpty(id))
            return;
        Apiservice apiservice = ApiUtil.getInstance().create();
        HashMap params = new HashMap();
        params.put("temp", temp);
        params.put("type", type);
        params.put("id", id);
        params.put("userid", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
        showProgress(getClass().getSimpleName());
        Call<ApiResponseBody> call = apiservice.doLike(params);
        call.enqueue(new Callback<ApiResponseBody>() {
            @Override
            public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                dismissProgress();
                if (response.isSuccessful()) {
                    selProbleList();
                } else {
                    showTost(response.raw().message(), 0);
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                dismissProgress();
                ApiException exception = ExceptionHandle.handleException(t);
                if (exception.isTokenInvalid()) {
                    showUnLoginSnackbar();
                } else if (exception.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else {
                    showTost(exception.getMessage(), 1);
                }
            }
        });

    }

    //回复问题
    private void upAnswer() {
        if (Util.isEmpty(PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID))) {
            showTost("未登录！请先登录", 1);
            return;
        }
        String answer = etComment.getText().toString().trim();
        if (Util.isEmpty(answer)) {
            showTost("请输入您的评论", 1);
            return;
        }
        if (answer.length() > 500) {
            showTost("请将评论控制在500字内，当前字数：" + answer.length(), 1);
            return;
        }
        if (!Utils.noContainsEmoji(answer)) {
            showTost("不支持输入emoji表情", 1);
            return;
        }
        Apiservice apiservice = ApiUtil.getInstance().create();
        HashMap params = new HashMap();
        params.put("userid", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
        params.put("id", mQuestionId);
        params.put("content", answer);
        if(TextUtils.isEmpty(deviceId)){
            ToastUtil.showToast(getActivity(),"设备唯一标识符异常");
            return;
        }
        params.put("deviceId", deviceId);
        params.put("platform", Constant.ANDROID_CHANNEL);
        if (PreferencesUtil.getBoolean(getActivity(), PreferencesUtil.KEY_IS_ENGINEER))
            params.put("isProjector", "1");
        else
            params.put("isProjector", "0");

        showProgress(getClass().getSimpleName());
        Call<ApiResponseBody> call = apiservice.projecterSubProblem(params);
        call.enqueue(new Callback<ApiResponseBody>() {
            @Override
            public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                dismissProgress();
                if (response.isSuccessful()) {
                    showTost("提交成功", 0);
                    commit.setVisibility(View.GONE);
                    getBaseActivity().hideSoftKeybord();
                    etComment.setText("");
                    commiting = false;
                    selProbleList();
                } else {
                    showTost(response.raw().message(), 1);
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                dismissProgress();
                ApiException apiException = ExceptionHandle.handleException(t);
                commiting = false;
                if (apiException.isTokenInvalid()) {
                    showUnLoginSnackbar();
                } else if (apiException.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else {
                    showTost(apiException.getMessage(), 0);
                }
            }
        });
    }

    //评论答案
    private void addDiscuss() {
        if (Util.isEmpty(PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID))) {
            showTost("未登录！请先登录", 1);
            return;
        }
        String answer = etComment.getText().toString().trim();
        if (Util.isEmpty(answer)) {
            showTost("请输入您的评论", 1);
            return;
        }
        if (answer.length() > 500) {
            showTost("请将评论控制在500字内，当前字数：" + answer.length(), 1);
            return;
        }
        if (!Utils.noContainsEmoji(answer)) {
            showTost("不支持输入emoji表情", 1);
            return;
        }
        if(TextUtils.isEmpty(deviceId)){
            ToastUtil.showToast(getActivity(),"设备唯一标识符异常");
            return;
        }
        Apiservice apiservice = ApiUtil.getInstance().create();
        HashMap params = new HashMap();
        params.put("id", answerId);
        params.put("discussContent", answer);
        params.put("userId", PreferencesUtil.getString(getContext(), PreferencesUtil.USER_ID));
        params.put("platform", "0");
        params.put("deviceId", deviceId);
        params.put("page", 1);
        params.put("toUserId", toUserId);
        showProgress(getClass().getSimpleName());
        Call<ApiResponseBody> call = apiservice.addDiscuss(params);
        call.enqueue(new Callback<ApiResponseBody>() {
            @Override
            public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                dismissProgress();
                if (response.isSuccessful()) {
                    showTost("提交成功", 0);
                    commit.setVisibility(View.GONE);
                    getBaseActivity().hideSoftKeybord();
                    toUserName.setText("");
                    etComment.setText("");
                    etComment.setHint("写评论");
                    toUserId = null;
                    commiting = false;
                    selProbleList();
                } else {
                    showTost(response.raw().message(), 1);
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                dismissProgress();
                commiting = false;
                ApiException apiException = ExceptionHandle.handleException(t);
                if (apiException.isTokenInvalid()) {
                    showUnLoginSnackbar();
                } else if (apiException.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else {
                    showTost(apiException.getMessage(), 0);
                }
            }
        });
    }

    //工程师删除自己评论
    private void delAnswerOrDiscusss(String id) {

        Apiservice apiservice = ApiUtil.getInstance().create();
        HashMap params = new HashMap();
        params.put("id", id);
        params.put("userId", PreferencesUtil.getString(getContext(), PreferencesUtil.USER_ID));
//        params.put("platform", "0");
//        params.put("deviceId", deviceId);
        params.put("page", 1);
        params.put("type", 1);
        showProgress(getClass().getSimpleName());
        Call<ApiResponseBody> call = apiservice.delAnswerOrDiscusss(params);
        call.enqueue(new Callback<ApiResponseBody>() {
            @Override
            public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                dismissProgress();
                if (response.isSuccessful()) {
                    showTost("提交成功", 0);
                    selProbleList();
                } else {
                    showTost(response.raw().message(), 1);
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                dismissProgress();
                ApiException apiException = ExceptionHandle.handleException(t);
                if (apiException.isTokenInvalid()) {
                    showUnLoginSnackbar();
                } else if (apiException.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else {
                    showTost(apiException.getMessage(), 0);
                }
            }
        });
    }
}
